/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isnmp.ui;

import isnmp.ui.dto.CPUInfo;
import isnmp.ui.dto.DeviceInfo;
import isnmp.ui.dto.StorageInfo;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;

import sg.simple.snmp.provider.IpDiscoveryCallback;
import sg.simple.snmp.provider.NetMapException;
import sg.simple.snmp.provider.SimpleSNMPQueries;
import sg.simple.snmp.provider.SimpleSNMPQueriesFactory;
import isnmp.ui.dto.MachineInfo;
import isnmp.ui.dto.ProgramInfo;
import isnmp.ui.dto.NetworkInfo;
import java.util.HashMap;
import java.util.Map;


public class NetMapManager {

	public static String SUBNET_MASK = "255.255.255.0";
	public static int REQUEST_TIME_OUT = 2000;
	public static String NETWORK_COMMUNITY = "public";
	private static NetMapManager netMapManager;
	private static SimpleSNMPQueries simpleSNMPQueries;


	public List<String> ipList;
	Vector<String> urls;
        private Map<ChildDevice, String> childDeviceList = null;

        
	Logger logger = Logger.getLogger(NetMapManager.class);

	private NetMapManager() {
                childDeviceList = new HashMap<ChildDevice, String>();
		ipList = new ArrayList<String>();
		urls = new Vector<String>();
		initFrameManager();

	}

	public void initFrameManager() {
		simpleSNMPQueries = SimpleSNMPQueriesFactory.instance().create(false);
	}

	static NetMapManager getInstance() {
		if (netMapManager == null) {
			netMapManager = new NetMapManager();
		}
		return netMapManager;

	}

	public List<String> getIps() {
		IpDiscoveryCallback callback = new IpDiscoveryCallback() {

			@Override
			public void onActiveIpDiscovered(String ip) {
				MainJFrame.getInstance().addChildToDeviceTree(ip);
			}

			@Override
			public void onError(NetMapException ex) {

			}

		};

		try {
			simpleSNMPQueries.listIPsAsync(SUBNET_MASK, REQUEST_TIME_OUT,
					callback);
		} catch (Exception ex) {
			logger.error(ex);
		}

		return ipList;
	}

	public Vector<String> getURLList() {
		return urls;
	}

	public static SimpleSNMPQueries getSimpleSNMPQueries() {
		return simpleSNMPQueries;
	}

	public static MachineInfo getMachineInfo(ChildDevice childDevice)
			throws NetMapException {

		return simpleSNMPQueries.getMachineInfo(childDevice.getIp(),
				NetMapManager.REQUEST_TIME_OUT, NETWORK_COMMUNITY);

	}
    static void loadChildDeviceData(ChildDevice childDevice) throws NetMapException {

        synchronized(childDevice){


        childDevice.setCpuInfoList(simpleSNMPQueries.getCPUInfo(childDevice.getIp(),
				REQUEST_TIME_OUT, NETWORK_COMMUNITY));


		childDevice.setStorageInfoList(simpleSNMPQueries.getStorageInfo(
				childDevice.getIp(), REQUEST_TIME_OUT, NETWORK_COMMUNITY));
			childDevice.setProgramInfoList(simpleSNMPQueries.getProgramInfo(childDevice.getIp(),
				REQUEST_TIME_OUT, NETWORK_COMMUNITY));
		childDevice.setNetworkInfoList(simpleSNMPQueries.getNeworkInfo(childDevice.getIp(),
				REQUEST_TIME_OUT, NETWORK_COMMUNITY));

		childDevice.setDeviceInfoList(simpleSNMPQueries.getDeviceInfo(childDevice.getIp(),
				REQUEST_TIME_OUT, NETWORK_COMMUNITY));
                childDevice.setMachineInfo( simpleSNMPQueries.getMachineInfo(childDevice.getIp(),
				REQUEST_TIME_OUT, NETWORK_COMMUNITY) );

	}
	}
        
        
	public static void setSubnetMask(String subnetMask) {
		SUBNET_MASK = subnetMask;
	}

	public static void setTimeout(int timeout) {
		REQUEST_TIME_OUT = timeout;
	}

	public static void setNetworkCommunity(String community) {
		NETWORK_COMMUNITY = community;
	}

	public static String getSubnetMask() {
		return SUBNET_MASK;
	}

	public static int getTimeout() {
		return REQUEST_TIME_OUT;
	}

	public static String getNetworkCommunity() {
		return NETWORK_COMMUNITY;
	}

        public Map<ChildDevice, String> getChildList()
        {
            return childDeviceList;
        }

}
